package com.exam.user.service;

import com.exam.common.entity.Request;
import com.exam.common.until.NumberUtils;
import com.exam.user.mapper.UserMapper;
import com.exam.user.pojo.User;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName : UserService
 * @Description :
 * @Author : WHB
 */

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX = "user:phone:";


    public Request checkData(String data, Integer type){
        User record = new User();
        //判断数据类型
        switch (type){
            case 1:     //用户名
                record.setUsername(data);
                break;
            case 2:     //手机号
                record.setPhone(data);
                break;
            default:
                return new Request(false,400,"参数有误");
        }
        //


        int count = userMapper.selectCount(record);

        return new Request(count == 0,200,"校验成功");
    };

    public void sendCode(String phone) {
        //生成key
        //生成验证码
        String key = KEY_PREFIX + phone;
        String code = com.exam.common.until.NumberUtils.generateCode(4);
        Map<String, String> msg = new HashMap<>();
        msg.put("phone",phone);
        msg.put("code", code);
        //发送验证码
        amqpTemplate.convertAndSend("exam.sms.exchange","sms.code",msg);

        //保存验证码到redis
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
    }

    public Request register(User user, String code) {
//        String cacheCode = redisTemplate.opsForValue().get(KEY_PREFIX + user.getPhone());
//        //检验验证码
//        if (!StringUtils.equals(code, cacheCode)){
//            return new Request(false,400,"验证码有误");
//        };
        String salt = "exam" + NumberUtils.generateCode(6) ;
        user.setSalt(salt);
        //对密码进行加密
        user.setPassword(DigestUtils.md5Hex(user.getPassword()+salt));
        //写入数据库
        user.setCreated(new Date());
        userMapper.insert(user);
        return new Request(true,200,"注册成功");
    }

    public User queryUser(String username, String password) throws Exception {
        User record = new User();
        record.setUsername(username);
        User user = userMapper.selectOne(record);

        //用户名不存在
        if (user == null){
            throw new Exception("用户名不存在");
        }

        //密码错误
        if (!StringUtils.equals(user.getPassword(),DigestUtils.md5Hex(password + user.getSalt()))){
            throw new Exception("密码错误");
        };

        // 正确
        return user;
    }

    public User queryUserById(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }
}
